use chrono::Local;
use serde::{Deserialize, Serialize};
use sqlx::types::chrono::DateTime;
use struct_field_names_as_array::FieldNamesAsSlice;
use crate::core::serialize_tools::empty_string_as_none;

// 新闻表单数据
#[derive(sqlx::FromRow, Deserialize, Serialize, FieldNamesAsSlice)]
pub struct BlogForm { 
    // 分类id
    pub cid: i64,
    // 名称
    pub title: String, 
    // SEO名称
    pub seo_title: String, 
    // SEO关键字
    pub seo_keywords: String, 
    // SEO描述
    pub seo_description: String, 
    // 描述
    pub description: String, 
    // 图片
    pub image: String,
    // 是否显示： 1显示
    pub is_show: i32,
    // 是否置顶 1置顶
    pub is_top: i32,
    // 点击
    pub hits: i64,
    // 内容
    pub content: String
}

// 新闻搜索
#[derive(Serialize, Deserialize)]
pub struct BlogListReq {
    pub page: Option<i64>,
    pub page_size: Option<i64>,
    #[serde(default, deserialize_with = "empty_string_as_none")]
    pub cid: Option<i64>,
    #[serde(default, deserialize_with = "empty_string_as_none")]
    pub is_show: Option<i32>,
    #[serde(default, deserialize_with = "empty_string_as_none")]
    pub is_top: Option<i32>,
    pub title: Option<String>
}

// 新闻列表行数据
#[derive(sqlx::FromRow, Deserialize, Serialize, FieldNamesAsSlice)]
pub struct BlogRow { 
    pub id: i64,
    pub title: String, 
    pub image: String,
    pub is_show: i32,
    pub is_top: i32,
    pub hits: i64,
    pub category_name: String,
    pub created_at: DateTime<Local>,
    pub updated_at: DateTime<Local>
}

// 新闻分页结果
#[derive(Serialize, Deserialize)]
pub struct BlogListResp {
    pub paginate: String,
    pub list: Vec<BlogRow>
}
